The mDialogBox Component: Not Your Father's Dialog Box
Phillip Kerman
As a developer, you know how convenient components are because you don't have build custom solutions for common tasks. Even more important, however, is what components mean to end users. Components offer users a consistent approach to getting their work done, so they don't have to figure out each and every custom interface element that developers create.
This imposed consistency is even truer with components in Macromedia Central because many styles are not customizable (for example, typefaces are not changeable). Although this means users will become familiar with Central applications quicker, it appears to mean that, as a developer, you're stuck with fewer options. In fact, however, whatever style limitations Central components have they more than make up for it by giving you tons of ways to customize their functionality.
Take the case of the mDialogBox component. Here you have a really sophisticated component in a seemingly simple package. Here are the most appropriate uses for mDialogBox:
- A modal dialog box, which the user can't proceed from until first making a selection
- An optional dialog box, though there are other ways to achieve this, such as regular buttons
- A multipage dialog box for related options such as user preferences
This article shows you how to build interactions such as these after first explaining how to populate and modify an mDialogBox component.
Requirements
To complete this tutorial you will need to install the following software and files:
Macromedia Central
Macromedia Central SDK
Macromedia Flash MX 2004
Tutorials and sample files:
mdialogbox.zip (106K)
Understanding the mDialogBox Structure
It's easiest to understand the structure of the mDialogBox component after you've seen what it can do. Figure 1 shows a fairly full-featured example, albeit mocked up with simple graphics. Because this dialog box has two sections (red and green), the user is given a drop-down menu from which to select one page to view. (The user sees only one page at a time.) Notice that because the two clips are sized differently, the dialog box will accommodate, by tweening this difference at the time the user makes a selection. The setting you make for the "grow from" parameter—left, right, middle—determines how this tween behaves. Finally, you can specify other details, including something other than "title of dialog" for the title and whether or not a close box appears. If you do forgo the close box you'll probably want to give the user a way to issue the doClose() script. Because you can populate the dialog box with clips, they can contain other components and easily issue any script you want (including doClose()—though be sure to check out the "mDialogBox Tips" page for ways to reference the dialog box from inside the dialog box).
Figure 1. One dialog box can include multiple pages accessible to the user through a drop-down menu.
Populating mDialogBox
You can populate mDialogBox manually, either by using the parameters tab in the Property inspector or by writing scripts. Like many other components, mDialogBox supports parallel arrays: one for labels and one for data. That is, the labels are what the user sees in the drop-down menu (red_label and green_label in Figure 1) and the data are strings that match the corresponding library items' linkage identifiers. To set the linkage identifier for the clip you want to appear in the dialog box, select the symbol in the library window and choose Linkage from the Library's options menu or the context menu when you right-click it (Control-click on Mac). If you then select Export for ActionScript you'll be able to set an identifier (see Figure 2).
Figure 2. Symbols used within the mDialogBox component need linkage properties set as shown.
You can manually populate the mDialogBox component's Labels and Data parameters through the Property inspector (as shown in Figure 3). Alternatively and, really, more optimally, you can populate using scripts by first making a Central DataProvider—basically an array full of ActionScript objects, each with a Label and Data property. Here's the script to make a DataProvider and populate mDialogBox for the simple examples shown so far:
var dp = new Central.DataProviderClass();
dp.addItem({label:"red_label", data: "red_linkage"});
dp.addItem({label:"green_label", data: "green_linkage"});
myDialog.setDataProvider(dp);
In order for that last line to work you need to give the mDialogBox component an instance name, "myDialog".
Figure 3. Although populating using a DataProvider is preferable, you can also populate mDialogBox manually with the Parameters tab of the Property inspector.
Using a One-Screen Dialog Box
There are other options too; after all, you don't have to use a multiscreen dialog box. If you want just one screen, you only need to set the parameter for "single content linkage id" (or create a DataProvider with a single item). A multiscreen dialog box is appropriate whenever there are many settings for the user to make—perhaps more options than comfortably fit on one small screen—or when the options fall into natural groupings. Use a single-screen dialog box when you need to present the user with a simple prompt, such as "Are you sure you want to clear the history?" followed by Yes and No buttons.
Deciding on Modality
Regardless whether you use a single- or multiscreen dialog box, you need to decide whether you want the dialog box to be modal or not. A modal dialog box, when displayed, disables all other interactive elements, such as buttons and text entry fields. In addition, the modal option covers everything under the dialog box with a gray transparent cover. The user cannot interact with the rest of your application—just the dialog box—when a modal dialog box is open. Although the default modal setting is false, I think most cases call for a modal dialog box. Without the modal option there are few benefits to mDialogBox over other Macromedia Central components such as the AccordionTab or ExpandingPod. In any event, you can set modal to true or false using the Parameters tab in the Property inspector or through the initially undocumented method setIsModal()—that is, myDialog.setIsModal(true) or myDialog.setIsModal(false).
Opening and Closing the Dialog Box
Without picking a favorite method, I definitely need to show you doClose() and doOpen(). I mention doClose() first because, without it, any dialog box you place on the Stage appears when the application first loads. Most likely you'll only want the dialog box to appear once the user selects Preferences from a menu. So just stick doClose() in your initialization scripts and later issue doOpen(). The following complete example displays a dialog box when the user selects Preferences from the menu:
myDialog.doClose()
showPreferences=function(){
myDialog.doOpen();
//probably also populate the dialog with the current preferences
}
Central.initApplication(this,this);
In order for the preferences to even appear in the menu, your product.xml
file needs the attribute hasPreferences="true" inside the
application tag.
Now that you know the basic possibilities of the mDialogBox component, you can look at a couple examples that exploit it for specific needs.
Putting mDialogBox to Work
The following two examples are complete and practical. The first (app_1.fla) is a simple confirmation dialog box; the second (app_2.fla) gives the user access to several preferences that get saved in a local shared object. I discuss in detail what's going on after the following steps.
Note: You can download the source files for these examples on the first page of this article, but you'll have to install the application into Central using a web server (as part of the security model of Central). Note that the productID used in the provided product.xml file is the one and only testing productID used in many other examples. Because you can't install two applications with the same productID, either be sure first to uninstall any sample applications (such as from other tutorials I've written) or replace productID with a unique number provided to you from the Macromedia Central Product ID Registration generator.
Simple Confirmation Dialog Box
This example creates the simple modal confirmation dialog box shown in Figure 4.
Figure 4. This simple modal confirmation dialog box results from the steps you follow.
- Select File > Publish Settings, click the Flash tab, and set the Version drop-down menu to Flash Player 6.
- To make the insides of your dialog box, drag two mPushButton components onto the Stage. Set one label to yes and its clickHandler to doYes and set the other label to no and its clickHandler to doNo. In addition, you can include a graphic question mark like the one I grabbed from the Application Graphics Library that ships with Macromedia DevNet Resource Kit Volume 4.
- Use the Text tool to draw a text field above the two buttons and make the margins wide. Set the text field to Dynamic and give it an instance name prompt_txt. Deselect the editable option and set Line Type to Multiline.
- Select everything and convert to symbol (press F8). Select the movie clip and be sure to click the top-left justification (shown later in Figure 6). Name it confirmation and click OK.
- Remove the instance of this confirmation symbol from the Stage (it's safe inside your Library). Select the item in the Library and choose Linkage from the Library's options menu or when you right-click it (Option-click on Mac). Click the Export for ActionScript option and leave the linkage identifier in its default "confirmation." Click OK.
- Drag the following components onto the Stage: two mPushButton, one mTextInputField, one mListBox, and one mDialogBox.
- Give one button the label add and the clickHandler doAddToList. Give the other button the label clear and the clickHandler doClear.
- Give mListBox the instance name myList and mTextInputField the instance name myText. (While you're on mTextInputField, you can set its Show Label parameter to false.)
- Click the first keyframe and paste the following code in it:
01 var dp = new Central.DataProviderClass();
02 dp.addItem({label:"n/a", data: "confirmation"});
03 myDialog.setDataProvider(dp);
04
05 myDialog.setIsModal(true);
06 myDialog.showClose(false);
07 myDialog.setTitle("confirmation");
08 myDialog.doClose();
09
10 doClear=function(){
11 myDialog.doOpen();
12 var dialogContent=myDialog.getSelectedContent();
13 dialogContent.prompt_txt.text="Are you sure?";
14
15 dialogContent.doNo=function(){
16 myDialog.doClose();
17 }
18
19 dialogContent.doYes=function(){
20 myList.removeAll();
21 myDialog.doClose();
22 }
23 }
24
25 doAddToList=function(){
26 myList.addItem(myText.getValue());
27 myText.setValue("");
28 }
29
30 Central.initApplication(this,this);
In the first three lines I create a DataProvider, populate it (with
just one item), and then associate the data with the myDialog instance.
Lines 5–8 do the following initialization: set it to modal, remove the
close box, set the title, and finally close it, so it's closed initially.
It's a good habit to close it after you populate it. In the case of setting
it modal, for example, it reappears even if you already closed it, so
it's a good habit to close it as the last step after populating it.
If you jump to line 25 you can see the Add button's change handler defined. Line 30 has the obligatory initApplication() call.
As shown in lines 10–23, when the user clicks the Clear button it triggers the doClear() function. Line 11 opens the dialog box and line 12 grabs a reference to a clip embedded inside the dialog box (using the built-in getSelectedConent() method). That reference (the homemade variable dialogContent) is used to populate a text field (line 13) and then define two callbacks (doNo() in line 15 and doYes() in line 19). Those are the click handlers for two buttons inside the confirmation clip. Both call the doClose() method but doYes() additionally calls the ListBox's removeAll() built-in method (line 20).
It's much easier to reach down into mDialogBox's content clips, using getSelectedContent(), than it is for the embedded clip to reference the dialog box itself. If for no other reason, the approach of talking down to the clip is better because nearly all the code is high up in the main timeline. (I discuss this topic more in the Tips section.)
Multiscreen Preferences Dialog Box
This example creates the multiscreen preferences dialog box shown in Figure 5 that contains three categories: gender, birthday, and software preferences.
Figure 5. This multiscreen preferences dialog box results from the steps you follow.
-
For the gender clip, drag one mPushButton component and two mRadioButton components onto the Stage. In addition, create some static text that reads "Pick your gender." Use the Property inspector to set the PushButton label to Next and Change Handler to doNext. (By the way, each of these three clips needs a copy of this button so once you've set the parameters make a duplicate on the Stage that you can copy and paste.) The mRadioButton components need the following parameters changed:
Label: Male (for Button 1), Female (for Button 2)
Group Name: genderGroup (for both buttons)
Data: 1 (for Button 1), 2 (for Button 2)
Change Handler: pickGender (for both buttons) - Select the text and buttons and press F8 to convert to a movie clip. Be sure to select the top-left registration option and name the symbol gender. You can remove it from the screen because later you'll set the linkage for all three clips.
- For the birthday preference, drag an mCalendar component onto the Stage. Give it the instance name birthday and change the selection type parameter to single. Set Change Handler to pickBirthday. Create a static text block that reads "Set your birthday." Finally, create a Next button the same as you did in Step 1 (label Next and Change Handler doNext). Select the calendar, text, and Next button and convert to symbol (press F8). Give this symbol the name birthday, click OK, and then delete the instance remaining on the Stage.
- The software preference uses three mCheckBox components (plus a Next button and static text for a title). Drag three instances of the mCheckBox component onto the Stage and give them the following instance names: software_1, software_2, and software_3. Set each one's Change Handler to pickSoftware. Finally, create a static text field that reads "Pick your software preferences." Make a Next button as you did in Steps 1 and 3. Finally, select the buttons and text and convert to symbol (press F8). Name the symbol software, click OK, and then delete the instance on the Stage.
- For each of the three symbols created (gender, birthday, and software), set a linkage identifier that matches their name. All you have to do is open the Library and select one at a time, pick Linkage from the Library's options menu, click the first check box for Export for ActionScript, and then click OK. Finally, if you're not using the same old file from the last exercise, drag an instance of the mDialogBox component and give it an instance name myDialog. Set the Modal Dialog parameter to true.
-
Click the first keyframe and paste the following code in it:
01 var dp = new Central.DataProviderClass(); 02 dp.addItem({label:"Gender", data: "gender"}); 03 dp.addItem({label:"Birthday", data: "birthday"}); 04 dp.addItem({label:"Software", data: "software"}); 05 myDialog.setDataProvider(dp); 06 07 myDialog.setTitle("Preferences"); 08 myDialog.doClose(); 09 showPreferences=function(){ 10 myDialog.doOpen(); 11 } 12 onActivate=function(){ 13 prefs_so=SharedObject.getLocal("prefs"); 14 } 15 16 var obj = new Object(); 17 obj.onContentChanged = function(ref){ 18 //get a reference to the dialog content 19 var dialogContent=myDialog.getSelectedContent(); 20 21 //=============populate: 22 //for gender: 23 var theirGender=prefs_so.data.gender 24 if(theirGender!=undefined){ 25 dialogContent.genderGroup.setValue(theirGender); 26 } 27 28 //for birthday preference: 29 var theirBirthday=prefs_so.data.birthday 30 if(theirBirthday!=undefined){ 31 dialogContent.birthday.setSelectedItem(theirBirthday); 32 dialogContent.birthday.setDisplayedMonth(theirBirthday); 33 } 34 35 //for software preference: 36 var theirSoftware=prefs_so.data.software; 37 if(theirSoftware==undefined){ 38 prefs_so.data.software=[0,0,0]; 39 theirSoftware=prefs_so.data.software; 40 } 41 dialogContent.software_1.setValue(theirSoftware[0]); 42 dialogContent.software_2.setValue(theirSoftware[1]); 43 dialogContent.software_3.setValue(theirSoftware[2]); 44 //give each button a "value" 45 dialogContent.software_1.value=0; 46 dialogContent.software_2.value=1; 47 dialogContent.software_3.value=2; 48 49 //=============set up callbacks 50 //for all: 51 dialogContent.doNext=function(){ 52 myDialog.setSelectedNextIndex(); 53 } 54 //for gender: 55 dialogContent.pickGender=function(ref){ 56 prefs_so.data.gender=ref.getData(); 57 } 58 //for birthday preference: 59 dialogContent.pickBirthday=function(ref){ 60 prefs_so.data.birthday=ref.getSelectedItems()[0]; 61 } 62 //for birthday preference: 63 dialogContent.pickSoftware=function(ref){ 64 prefs_so.data.software[ref.value]=ref.getValue(); 65 } 66 } 67 myDialog.addListener(obj) 68 69 Central.initApplication(this,this);
This time creating the DataProvider and associating it with the dialog box uses lines 1–5, because there are three screens. Lines 7 and 8 simply set the title and close the dialog box initially. Lines 9–11 set up the callback that Central calls if the user chooses to view preferences. When this happens we just open the dialog box (line 10). Lines 12–14 trap the onActivate method that Central triggers once the application initializes. Here we just grab the shared object in the "prefs" file. Way at the bottom (line 69) we initialize the Central application.
The bulk of code is stored inside the onContentChanged event listener
on the obj object, which is first defined on line 16 and
later added as a listener to the dialog box on line 67. That is, every
time the content changes (including whenever the dialog box simply opens)
we run all the code from lines 18–65. This may seem like overkill—after
all, if users are only opening the dialog box to see their gender preference
there is no need to set up the birthday options—but unless you want to
check which section of the dialog box it has changed to, then this code
is fine even if some of it is superfluous.
There are two main tasks I perform every time the dialog box changes:
populate the clip to reflect its previous settings and define the click/change
handlers for the various buttons contained. First consider the population
portion (lines 21–47). In order to do this I start on line 19 by grabbing
a reference to the selected clip. For the gender I first see what value
is stored in the gender property of the shared object (line
23). Naturally, this is undefined initially but, if not, I set the value
for the RadioButton group called genderGroup accordingly (line 25). Similarly,
the birthday preference is accessed in line 29 and, provided it's not undefined,
I trigger two mCalendar component methods: setSelectedItem() to select
the date and setDisplayedMonth() so you can see the date. Populating
the radio buttons is a tad more involved. I designed the software property
to contain an array. Actually, you can see in lines 38–39 that if that
property is indeed undefined, I initialize it with an array full of zeros.
Lines 41–43 use the built-in setValue() method to make the buttons' check
marks appear. Lines 45–47 create and set a property I made up called value.
That is, check boxes don't really have a value property
but, as you'll see later (on line 64), this makes it easy to determine
which button was clicked.
For the click handlers (lines 49–65) you can see some consistencies.
The doNext() method (lines 51–53) simply triggers the built-in setSelectedNextIndex()
which moves the mDialogBox component to the next page of a multiscreen
display, as if the user used the drop-down menu to go to the next screen.
Remember that the buttons inside the three clips trigger one of the three
change handlers: pickGender(), pickBirthday(), and pickSoftware(). When
a Central component triggers a change handler, it passes a reference
of itself as a parameter. So in line 56 I just use the built-in RadioButton
method getData() when setting the gender property. Line
60 uses the Calendar's getSelectedItems() method when setting the birthday property
(but do notice that we only need the first item—in the zero slot—of the
array returned). Line 64 is slightly more tricky because we need to know
which slot in the array for the software property to set—that's
where the homemade value property (that I created in lines
45–47) comes into play.
I like this example because even though you might not need to store the user's gender and birthday, you can apply and expand this structure to any set of preferences. This exercise lets you play with a couple of the other components too!
mDialogBox Tips
No doubt it's unlikely that the way I solved these dialog box tasks are identical to how you would have coded it. Although this code works, I'm sure as you put the Central components to the test you'll develop your own efficiencies. Here are just a few tips that may save you some time.
Choose Top-Left Registration
Use the top-left registration option when creating clips (as shown in Figure 6). At least when defining the linkage identifiers for the mDialogBox component, make sure your content bleeds down and to the right.
Figure 6. Although you might like another registration option, the top left is the one to use when creating content for components, including mDialogBox.
Don't Bother Learning the Hierarchy, Use Methods
In the exercises in this article I always "talked down" to the symbols contained in the dialog box. If you want to put code inside that "talks up" the hierarchy to the component itself (that is, the dialog box instance) then it's best to pass a reference to that component down when the dialog box opens or changes. Something like this right after you open the dialog box:
myDialog.getSelectedContent().theController=myDialog;
This way, from code inside the symbol embedded in the dialog box, you can just use theController.doClose() to close the dialog box.
An alternative, if less than ideal, way would be to do something like this:
theController=this._parent._parent;
doClose_btn.onRelease=function(){
theController.doClose();
};
Although this works, it relies on a specific hierarchy that may change in future revisions of the components. Besides, Central components are stored in remote shared libraries and can be updated independent of your application.
Set Up a Local Testing Environment
Many people complain that you can't see anything you're doing while working on Central applications. That is, Test Movie doesn't work because you have to test inside the Central environment. I used to go through a number of hoops to test inside Flash; for example, I'd download a copy of CentralComponents.swf and change each component's linkage to point to the local copy (then change it back when I was ready to distribute it).
The thing is, with just a couple extra steps you can indeed test inside Central! Start by installing the first revision of your application from localhost or another real server. Hunt down where the SWFs were installed (for example, inside the folder "C:\Documents and Settings\Phillip\Application Data\Macromedia\Central") and then overwrite those files when you publish. All you have to do is set the filename and destination from the Publish Settings dialog box.
At first glance the mDialogBox component seems too simple to learn much about. However, it's actually pretty cool—and quite deep. The big lesson you should learn here is that there are even more gems hidden among other components in Central. The time invested to learn how they work will pay you back over time.
About the author
Phillip Kerman is a Macromedia Flash programmer and author of the best-selling Sams Teach Yourself Macromedia Flash 24 Hours, ActionScripting in Flash, and the new book Flash MX 2004 for Rich Internet Applications. He also conducts Macromedia Flash training workshops around the world. He used Macromedia Flash Communication Server MX and Flash Remoting to program a real-time auction for www.stampedecattle.com that sells millions of dollars worth of cattle per hour. His site www.phillipkerman.com includes several Communication Server examples.